# coding=utf-8
from common.utils.types import Enum
from common import orm
from common.lottery import LOTTERY_TYPE

CP11X5_LOTTERY_TYPES = (
    LOTTERY_TYPE.SD_11X5,
    LOTTERY_TYPE.GD_11X5,
    LOTTERY_TYPE.SH_11X5,
    LOTTERY_TYPE.JX_11X5,
    LOTTERY_TYPE.FF_11X5,
)

BET_TYPE = Enum({
    "TWO_ANY": (1L, u"任选二"),
    "THREE_ANY": (2L, u"任选三"),
    "FOUR_ANY": (3L, u"任选四"),
    "FIVE_ANY": (4L, u"任选五"),
    "SIX_ANY": (5L, u"任选六"),
    "SEVEN_ANY": (6L, u"任选七"),
    "EIGHT_ANY": (7L, u"任选八"),
    "ONE_FRONT": (8L, u"前一"),
    "TWO_FRONT_DIRECT": (9L, u"前二直选"),
    "TWO_FRONT_GROUP": (10L, u"前二组选"),
    "THREE_FRONT_DIRECT": (11L, u"前三直选"),
    "THREE_FRONT_GROUP": (12L, u"前三组选"),
    "ONE_ANY": (13L, u"任选一"),
})


class Activity(object):
    id = orm.Column(orm.Integer, primary_key=True)
    term = orm.Column(orm.VARCHAR)  # 期号
    number = orm.Column(orm.VARCHAR)  # 开奖结果
    reference = orm.Column(orm.TEXT)  # 爬取结果页url
    status = orm.Column(orm.SmallInteger)  # 状态
    start_ts = orm.Column(orm.Integer)  # 我方开始时间戳（整点）
    end_ts = orm.Column(orm.Integer)  # 我方结束时间戳（略早于官方）
    announce_ts = orm.Column(orm.Integer)  # 爬取到结果的时间戳
    repeat_numbers = orm.Column(orm.Integer)
    created_at = orm.Column(orm.DATETIME)
    updated_at = orm.Column(orm.DATETIME)


class Trend(object):
    id = orm.Column(orm.Integer, primary_key=True)
    term = orm.Column(orm.VARCHAR)
    number = orm.Column(orm.VARCHAR)
    idx_0 = orm.Column(orm.VARCHAR)  # 第一位
    idx_1 = orm.Column(orm.VARCHAR)  # 第二位
    idx_2 = orm.Column(orm.VARCHAR)  # 第三位
    idx_3 = orm.Column(orm.VARCHAR)  # 第四位
    idx_4 = orm.Column(orm.VARCHAR)  # 第五位
    # 任选二到任选八，遗漏值一致
    miss_1 = orm.Column(orm.TEXT)
    # 前一，前二，前三直选，直接用idx
    miss_10 = orm.Column(orm.TEXT)  # 前二组选
    miss_12 = orm.Column(orm.TEXT)  # 前三组选
    repeat_numbers = orm.Column(orm.Integer)  # 和上一期重号个数

    @classmethod
    def get_defaults(cls):
        data = {}
        for k in range(5):
            data['idx_%s' % k] = ','.join('x' * 11)

        data['miss_1'] = data['miss_10'] = data['miss_12'] = ','.join(
            'x' * 11)
        return data

    def get_stat(self, bet_type):
        ''' 根据玩法返回统计（遗漏、冷热、最大和上次）数据
            这个数据应该被缓存起来（因为冷热和最大需要实时计算）
            对于每一期而言，这个数据是固定的
        '''
        resp = {}
        if bet_type in {1, 10, 12}:
            resp['miss'] = getattr(self, 'miss_%s' % bet_type)
        elif 2 <= bet_type <= 7 or bet_type == 13:
            resp['miss'] = self.miss_1
        elif bet_type == 8:
            resp['miss'] = self.idx_0
        elif bet_type == 9:
            resp['miss'] = '|'.join([self.idx_0, self.idx_1])
        elif bet_type == 11:
            resp['miss'] = '|'.join([self.idx_0, self.idx_1,
                                     self.idx_2])

        return resp
